<?php
/*
Plugin Name: RossDev Products
Description: Product helper for Cart66.
Author: Brian Ross
*/

// lets turn off autosave
wp_deregister_script( 'autosave' );

function rossdev_remove_schedule_delete() {
    remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
}
add_action( 'init', 'rossdev_remove_schedule_delete' );


// remove quick edit from product list
function remove_quick_edit( $actions ) {
	global $post;
	if( $post->post_type == 'rossdev_products' ) {
		unset($actions['inline hide-if-no-js']);
	}
    return $actions;
}

add_filter( 'post_row_actions', 'remove_quick_edit',10,2 );

// add product custom post type
function create_rossdev_products() {
	$labels = array(
		'name'					=> __( 'Products', 'custom post type generic name' ), 
		'singular_name'			=> __( 'Product', 'individual custom post type name' ),
		'add_new'				=> __( 'Add New', 'add' ),
		'add_new_item'			=> __( 'Add New Product' ),
		'edit_item'				=> __( 'Edit Product' ),
		'new_item'				=> __( 'New Product' ),
		'view_item'				=> __( 'View Product' ),
		'search_items'			=> __( 'Search Products' ),
		'not_found'				=> __( 'No Product Found' ),
		'not_found_in_trash'	=> __( 'No Product Found in trash' ),
		'parent_item_colon'		=> ''
	);

	$create_rossdev_products_args = array(
		'labels'			=> $labels,
		'public'			=> true,
		'capability_type'	=> 'post',
		'hierarchical'		=> false,
		'query_var'			=> true,
		'rewrite'			=> array(
									'slug'			=> 'products',
									'with_front'	=> false
								),
		'supports'			=> array(
									'title',
									'editor',
									'thumbnail',
									'categories'
								)
    );
    
	// register the post type
	register_post_type( 'rossdev_products', $create_rossdev_products_args );
				
	// register the taxonomy
	register_taxonomy( 'rossdev_collection', array ( 0 => 'rossdev_products' ) ,array(
		'hierarchical' => true,
		'label' => 'Collections',
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array(
						'slug' => 'collection'),
		'singular_label' => 'Collection')
	);

}
// add post type to core
add_action( 'init', 'create_rossdev_products' );

$sendback = wp_get_referer();
if ( strpos($sendback, 'post.php') !== false || strpos($sendback, 'post-new.php') !== false ) {
	$sendback = admin_url('edit.php');
	$sendback .= ( !empty( $post_type ) ) ? '?post_type=' . $post_type : '';
} else {
	$sendback = remove_query_arg( array('trashed', 'untrashed', 'deleted', 'ids'), $sendback );
}

// Delete Cart66 Attachment
function rossdev_delete_cart66() {
	global $wpdb;
	global $post;
	if( $post->post_type == 'rossdev_products' ) {
		$myTableID = $wpdb->prefix . 'cart66_products';
		$cart66key = get_post_meta( $post->ID, 'cart66ID', true );
		$wpdb->query( 'DELETE FROM ' . $myTableID . ' WHERE ID = "' . $cart66key .'"' );
		$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); 
		$attachments = get_posts($args);
		if ($attachments) {
			foreach ( $attachments as $attachment ) {
			wp_delete_attachment( $attachment->ID,true );
			}
		}
	}
}
add_action('before_delete_post','rossdev_delete_cart66');

// update post type function
function update_product_options(){
	global $post;
	global $wpdb;
	
	// lets only update rossdev_products meta data
	if( $post->post_type == 'rossdev_products' ) {
		// check for cart66ID then update
		if( $_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['cart66ID'] ) {
			// update cart66id second pass
			$data_array = array( 'name' => $_POST['post_title'], 'price' => $_POST['product_price'] , 'item_number' => $_POST['cart66ID'], 'max_quantity' => '1' );
			$id_array = array('ID' => $_POST['cart66ID']);
			$wpdb->update($wpdb->prefix . 'cart66_products', $data_array, $id_array );
			update_post_meta( $post->ID, 'cart66ID', $_POST['cart66ID'] );
			update_post_meta( $post->ID, 'product_price', $_POST['product_price'] );
		} elseif ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			// add cart66id first pass
			$data_array = array( 'name' => $_POST['post_title'], 'price' => $_POST['product_price'], 'max_quantity' => '1' );
			$wpdb->insert( $wpdb->prefix . 'cart66_products', $data_array );
			$rd_insert_id = $wpdb->insert_id;
			update_post_meta( $post->ID, 'cart66ID', $rd_insert_id );
			update_post_meta( $post->ID, 'product_price', $_POST['product_price'] );
			$data_array = array( 'item_number' => $rd_insert_id );
			$id_array = array('ID' => $rd_insert_id );
			$wpdb->update($wpdb->prefix . 'cart66_products', $data_array, $id_array );
		}
	}
}

// add update method for post type to core
add_action('save_post', 'update_product_options');

// create Cart66 meta box 
function product_attributes(){
	global $post;
	$custom = get_post_custom($post->ID);
?>
	<p>Product Price: $<input type="text" name="product_price" value="<?=$custom['product_price'][0]?>" size="10"/></p>
<?php
	if($custom['cart66ID']){
?>
		<p>Cart66 ID / Product Item Number: <a href="admin.php?page=cart66-products&task=edit&id=<?=$custom['cart66ID'][0]?>"><?=$custom['cart66ID'][0]?></a><input type="hidden" name="cart66ID" value="<?=$custom['cart66ID'][0]?>"/></p>	
<?php
	}
}

// add rossdev_products custom meta boxes
function product_listing_boxs() {
	// move featured image from sidebar
	remove_meta_box( 'postimagediv', 'rossdev_products', 'side' );
	add_meta_box( 'postimagediv', __('Product Image'), 'post_thumbnail_meta_box', 'rossdev_products', 'normal', 'high' );
	
	// add Cart66 meta box
	add_meta_box( 'productcart66meta', __('Product Attributes'), 'product_attributes', 'rossdev_products', 'normal', 'high' );
}

// add custom meta boxes to rossdev_products
add_action( 'do_meta_boxes', 'product_listing_boxs' );